package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.Result;
import com.example.reggie.domain.Dish;
import com.example.reggie.domain.Setmeal;
import com.example.reggie.domain.SetmealDish;
import com.example.reggie.dto.SetmealDto;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.DishService;
import com.example.reggie.service.SetmealDishService;
import com.example.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/setmeal")
@RestController
public class SetMealController {

    @Autowired
    SetmealService setmealService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    DishService dishService;


    // 套餐分页查询
    @GetMapping("/page")
    Result<Page> setmealPageList(Integer page,Integer pageSize,String name){
// 构造分页构造器
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        // 构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(name!=null,Setmeal::getName,name);
        // 添加排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> setmealList = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        setmealDtoPage.setRecords(setmealDtoList);
        for (int i=0;i<setmealList.size();i++){
            SetmealDto dishDto = new SetmealDto();
            BeanUtils.copyProperties(setmealList.get(i),dishDto);
            setmealDtoPage.getRecords().add(dishDto);
            String categoryName = categoryService.getById(setmealList.get(i).getCategoryId()).getName();
            if (categoryName!=null){
                setmealDtoPage.getRecords().get(i).setCategoryName(categoryName);
            }
        }
        return Result.success(setmealDtoPage);
    }


    // 添加套餐接口
    @PostMapping
    Result<String> setmealAdd(@RequestBody SetmealDto setmealDto){
        setmealService.setmealAdd(setmealDto);
        return Result.success("添加成功");
    }


    // 根据id查询套餐信息
    @GetMapping("/{id}")
    Result<SetmealDto> setmealGetById(@PathVariable Long id){
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = setmealService.getById(id);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(list);
        return Result.success(setmealDto);
    }

    // 修改套餐信息
    @CacheEvict(value = "setmealCache",allEntries = true)   // 清除缓存
    @PutMapping
    public Result<String> setmealUpdate(@RequestBody SetmealDto setmealDto){
        setmealService.setmealUpdate(setmealDto);
        return Result.success("添加成功");
    }

    // 批量修改套餐状态
    @CacheEvict(value = "setmealCache",allEntries = true)   // 清除缓存
    @PostMapping("/status/{status}")
    public Result<String> setmealStattusChange(@PathVariable Integer status, Long[] ids){
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        for (int i=0;i< ids.length;i++){
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getId,ids[i]);
            setmealService.update(setmeal,queryWrapper);
        }
        return Result.success("修改成功");
    }

    // 批量删除套餐
    @CacheEvict(value = "setmealCache",allEntries = true)   // 清除缓存
    @DeleteMapping
    public Result<String> setmealDelete(Long[] ids){
        setmealService.removeByIds(Arrays.asList(ids));
        for (int i=0;i<ids.length;i++){
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId,ids[i]);
            setmealDishService.remove(queryWrapper);
        }
        return Result.success("删除成功");
    }

    // 根据分类id查询套餐信息

    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status")  // 在缓存中查找，找不到则添加到缓存中
    @GetMapping("/list")
    public Result<List> setmealList(Long categoryId, Integer status){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        queryWrapper.eq(status!=null,Setmeal::getStatus,status);
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        return Result.success(setmealList);
    }

    @GetMapping("/dish/{id}")
    Result<Dish> setmealDishGetById(@PathVariable Long id){
        Dish dish = dishService.getById(id);
        return Result.success(dish);
    }


}
